import 'package:flutter/material.dart';

class LoginInput extends StatefulWidget {
  final String title;
  final String? hint;
  final ValueChanged<String>? onChanged;
  final ValueChanged<bool>? focusChanged;
  final bool lineStretch;
  final bool obscureText;
  final TextInputType keyboardType;

  const LoginInput(
      {super.key,
      required this.title,
      this.hint,
      this.onChanged,
      this.focusChanged,
      this.lineStretch = false,
      this.obscureText = false,
      this.keyboardType = TextInputType.text});

  @override
  State<LoginInput> createState() => _LoginInputState();
}

class _LoginInputState extends State<LoginInput> {
  final _focusNode = FocusNode();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _focusNode.addListener(() {
      print("Has focus: ${_focusNode.hasFocus}");
      if (widget.focusChanged != null) {
        widget.focusChanged?.call(_focusNode.hasFocus);
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          children: [
            Container(
              padding: EdgeInsets.only(left: 15),
              width: 100,
              child: Text(
                widget.title,
                style: const TextStyle(fontSize: 16),
              ),
            ),
            Expanded(
                child: TextField(
              focusNode: _focusNode,
              cursorColor: Colors.pink,
              obscureText: widget.obscureText,
              keyboardType: widget.keyboardType,
              autofocus: !widget.obscureText,
              onChanged: widget.onChanged,
              style: const TextStyle(
                  fontSize: 16,
                  color: Colors.black,
                  fontWeight: FontWeight.w300),
              decoration: InputDecoration(
                  contentPadding: const EdgeInsets.only(left: 20, right: 20),
                  hintText: widget.hint,
                  border: InputBorder.none,
                  hintStyle: const TextStyle(fontSize: 15, color: Colors.grey)),
            )),
          ],
        ),
        Padding(
          padding: EdgeInsets.only(left: widget.lineStretch ? 0 : 15),
          child: const Divider(
            height: 1,
            thickness: 0.5,
          ),
        )
      ],
    );
  }
}
